
// 引入axios文件
import axios from 'axios';
// 引入url
import { SERVICE_URL, APP_ID } from 'common/js/config'

/**
 * @author yang
 * @desc 隐藏电话号码
 * @param {Number} phone 当前需要隐藏的电话号码
 */
export const  formatPhone = (phone) => {
     phone += '';
     return phone.replace(/(\d{3})\d*(\d{4})/g, '$1***$2')
 }

/**
 * @author yang
 * @desc 存储localStorage
 * @param {String} key 存储的属性 value {String | Number | Array | Object} 存储的值
 */
export const setStorage = (key, value) => {
  if (!key) return;
  if (typeof value !== 'string') {
    value = JSON.stringify(value)
  }
  window.localStorage.setItem(key, value);
}

/**
 * @author yang
 * @desc 获取url链接后面的参数
 * @param {String} name 获取那个key的值
 * @return 返回值
 */
export const getUrlParams = (name) => {
  let _reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"),
    _regNext = window.location.search.substr(1).match(_reg);
  if (_regNext != null) return decodeURI(_regNext[2]) || '';
  else return '';
}

export const fnWxLogin = () => {
  axios.get(SERVICE_URL + 'user/v1/wxlogin/?code=' + code + '&state=slfn').then(res => {
    if (res.data && res.data.id) {
      setlocalStorage('wxUserInfo', res.data)
    }
  })
}

export const fnWxCation = () => {
  window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APP_ID + '&redirect_uri=' + 'http://wap.sailafeinav.com/' + '&response_type=code&scope=snsapi_userinfo#wechat_redirect';
}


/**
 * @author yang
 * @desc 清除缓存时间
 * @param {date} timestamp 时间戳
 * @return 返回存储的值
 */
export const closeTime = (timestamp) => {
  if (new Date(timestamp).toDateString() !== new Date().toDateString()) {
    setStorage('userInfoTimesTamp', new Date().getTime())
    removeStorage('wxUserInfo')
  }
}

/**
 * @author yang
 * @desc 获取localStorage
 * @param {String} key 获取的属性名称
 * @return 返回存储的值
 */
export const getStorage = name => {
  if (!name) return;
  return window.localStorage.getItem(name);
}

/**
 * @author yang
 * @desc 删除localStorage
 * @param {String} key 删除的属性名称
 */
export const removeStorage = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
}

/**
 * @author yang
 * @desc 获取style样式
 * @param {Object} element 元素对象
 * @param {String} attr 元素属性
 */
export const getStyle = (element, attr, NumberMode = 'int') => {
  let target;
  // scrollTop 获取方式不同，没有它不属于style，而且只有document.body才能用
  if (attr === 'scrollTop') {
    target = element.scrollTop;
  } else if (element.currentStyle) {
    target = element.currentStyle[attr];
  } else {
    target = document.defaultView.getComputedStyle(element, null)[attr];
  }
  //在获取 opactiy 时需要获取小数 parseFloat
  return NumberMode == 'float' ? parseFloat(target) : parseInt(target);
}


/**
 * @author yang
 * @desc 显示返回顶部按钮，开始、结束、运动 三个过程中调用函数判断是否达到目标点
 * @param {Function} callback  调用当前回调返回状态给外部接受状态
 */
export const showBack = callback => {
  let requestFram;
  let oldScrollTop;
  document.addEventListener('scroll', () => {
    showBackFun();
  }, false)
  document.addEventListener('touchstart', () => {
    showBackFun();
  }, {
    passive: true
  })

  document.addEventListener('touchmove', () => {
    showBackFun();
  }, {
    passive: true
  })

  document.addEventListener('touchend', () => {
    oldScrollTop = document.body.scrollTop;
    moveEnd();
  }, {
    passive: true
  })

  const moveEnd = () => {
    requestFram = requestAnimationFrame(() => {
      if (document.body.scrollTop != oldScrollTop) {
        oldScrollTop = document.body.scrollTop;
        moveEnd();
      } else {
        cancelAnimationFrame(requestFram);
      }
      showBackFun();
    })
  }

  //判断是否达到目标点 到达之后显示按钮
  const showBackFun = () => {
    if (document.body.scrollTop > 500) {
      callback(true);
    } else {
      callback(false);
    }
  }
}
